package com.irdstudio.efp.cus.api.rest;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.cus.service.facade.CusIndivCashIncomeService;
import com.irdstudio.efp.cus.service.vo.CusIndivCashIncomeVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.PubConstant;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class CusIndivCashIncomeController extends AbstractController {

	@Autowired
	@Qualifier("cusIndivCashIncomeService")
	private CusIndivCashIncomeService cusIndivCashIncomeService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value = "/cus/indiv/cash/incomes", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<CusIndivCashIncomeVO>> queryCusIndivCashIncomeAll(@RequestBody CusIndivCashIncomeVO vo) {
		setUserInfoToVO(vo);
		// 根据用户角色获取用户权限方案
		List<CusIndivCashIncomeVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);

		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = cusIndivCashIncomeService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = cusIndivCashIncomeService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = cusIndivCashIncomeService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = cusIndivCashIncomeService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
	}

	/**
	 * 根据主键查询详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/cus/indiv/cash/income/{incSerno }", method = RequestMethod.GET)
	public @ResponseBody ResponseData<CusIndivCashIncomeVO> queryByPk(@PathVariable("incSerno ") String incSerno) {
		CusIndivCashIncomeVO inVo = new CusIndivCashIncomeVO();
		inVo.setIncSerno(incSerno);
		CusIndivCashIncomeVO outputVo = cusIndivCashIncomeService.queryByPk(inVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键删除信息
	 * 
	 * @param cusIndivCashIncome
	 * @return
	 */
	@RequestMapping(value = "/cus/indiv/cash/income", method = RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody CusIndivCashIncomeVO inCusIndivCashIncomeVo) {
		int outputVo = cusIndivCashIncomeService.deleteByPk(inCusIndivCashIncomeVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * 
	 * @param inCusIndivCashIncomeVo
	 * @return
	 */
	@RequestMapping(value = "/cus/indiv/cash/income", method = RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody CusIndivCashIncomeVO inCusIndivCashIncomeVo) {
		int outputVo = cusIndivCashIncomeService.updateByPk(inCusIndivCashIncomeVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * 
	 * @param inCusIndivCashIncomeVo
	 * @return
	 */
	@RequestMapping(value = "/cus/indiv/cash/income", method = RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertCusIndivCashIncome(@RequestBody CusIndivCashIncomeVO inCusIndivCashIncomeVo) {
		int outputVo = cusIndivCashIncomeService.insertCusIndivCashIncome(inCusIndivCashIncomeVo);
		ResponseData<Integer> responseData = new ResponseData<Integer>();
		if (outputVo == -2) {
			responseData.setRows(outputVo);
			responseData.setMessage("操作失败,必须填写代发工资!");
			responseData.setCode(outputVo + "");
			responseData.setSuccess(false);
			return responseData;
		}
		return getResponseData(outputVo);

	}
}
